/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.pdx.cs.guavadata.domain;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.lang.String;
import java.sql.ResultSet;

/**
 *
 * @author VuTran
 */
public class Integer extends Domain {

    private java.lang.Integer min;
    private java.lang.Integer max;
    
    public int getMax() {
        return max;
    }
    
    public void setMax(int value) {
        max = value;
    }
    
    public int getMin() {
        return min;
    }
    
    public void setMin(int value) {
        min = value;
    }

    public Integer() {
        this.min = null;
        this.max = null;
    }

    public Integer(int min, int max) {
        this.min = min;
        this.max = max;
    }
    
    @Override
    public boolean validate(Object o) {
        java.lang.Integer i = null;
        
        if (o == null) return true;
        if (o instanceof java.lang.Integer) i = (java.lang.Integer) o;
        
        // If still null at this point, then o isn't a valid byte-worthy object
        if (i == null) return false;
        if ((min != null) && (min > i)) return false;
        if ((max != null) && (max < i)) return false;
        return true;
    }

    @Override
    public Object convertString(String s) throws DomainConversionException {
        if ((s == null) || (s == "")) return null;
        java.lang.Integer d;
        try {
            d = java.lang.Integer.parseInt(s);
        } catch (NumberFormatException e) {
            throw new DomainConversionException("Could not convert " + s + " into a 32-bit integer.");
        }
        
        if ((min != null) && (min > d)) throw new DomainConversionException("Value " + s + " is too small for a domain with minimum value " + min.toString() + ".");
        if ((max != null) && (max < d)) throw new DomainConversionException("Value " + s + " is too big for a domain with minimum value " + max.toString() + ".");
        return d;
    }

    @Override
    public Class getNaiveType() {
        return java.lang.Integer.class;
    }

    @Override
    public String getDBType() {
        return "int";
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Integer)) return false;
        Integer that = (Integer) obj;
        return ((this.max == that.max) && (this.min == that.min));
    }

    @Override
    public int hashCode() {
        return Integer.class.hashCode() * (min.hashCode() + 1) + max.hashCode();
    }

    @Override
    public String toString() {
        return "Integer";
    }

    @Override
    public Domain upcast() {
        return new BigInteger(min.longValue(), max.longValue());
    }

    @Override
    public Object upcastValue(Object o) throws Exception {
        if (o == null) return null;
        if (o instanceof java.lang.Integer) return (Long) o;
        return null;
    }

    @Override
    public Domain merge(Domain d) {
        if (!(d instanceof Integer)) return null;
        Integer that = (Integer) d;
        java.lang.Integer nmax = (this.max == that.max) ? this.max : null;
        java.lang.Integer nmin = (this.min == that.min) ? this.min : null;
        return new Integer(nmin, nmax);
    }

    @Override
    public boolean isOrdered() {
        return true;
    }

    @Override
    public boolean canContain() {
        return false;
    }

    @Override
    public Domain clone() {
        return new Integer(this.min, this.max);
    }

    @Override
    public Element ToXml(Document xdoc) {
        Element xe = xdoc.createElement("Domain");
        xe.setAttribute("Type", "Integer");
        if (max != null) xe.setAttribute("MaxValue", max.toString());
        if (min != null) xe.setAttribute("MinValue", min.toString());
        return xe;
    }

    public static Integer fromElement(Element e) {
        Integer o = new Integer();
        if (e.hasAttribute("MaxValue")) {
            o.max = java.lang.Integer.parseInt(e.getAttribute("MaxValue"));
        }
        if (e.hasAttribute("MinValue")) {
            o.min = java.lang.Integer.parseInt(e.getAttribute("MinValue"));
        }
        return o;
    }

    public static Integer FromDBInfoRow(ResultSet dr) {
        Integer o = new Integer();
        return o;
    }
}
